package org.jeecg.modules.demo.baseorderproject.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.baseorderproject.entity.BaseOrderProject;
import org.jeecg.modules.demo.baseorderproject.entity.BaseOrderProjectMain;
import org.jeecg.modules.demo.baseorderproject.entity.BaseOrderProjectWriteoff;
import org.jeecg.modules.demo.baseorderproject.service.IBaseOrderProjectService;
import org.jeecg.modules.demo.projectmanagement.entity.ProjectManagement;
import org.jeecg.modules.demo.projectmanagement.service.IProjectManagementService;
import org.jeecg.modules.util.DateUtils;
import org.jeecg.modules.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 订单项目表
 * @Author: jeecg-boot
 * @Date: 2022-02-11
 * @Version: V1.0
 */
@Api(tags = "订单项目表")
@RestController
@RequestMapping("/baseorderproject/baseOrderProject")
@Slf4j
public class BaseOrderProjectController extends JeecgController<BaseOrderProject, IBaseOrderProjectService> {
    @Autowired
    private IBaseOrderProjectService baseOrderProjectService;
    @Autowired
    private IProjectManagementService projectManagementService;
    @Autowired
    private RedisUtils redisUtils;

    /**
     * 分页列表查询
     *
     * @param baseOrderProject
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "订单项目表-分页列表查询")
    @ApiOperation(value = "订单项目表-分页列表查询", notes = "订单项目表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(BaseOrderProject baseOrderProject,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<BaseOrderProject> queryWrapper = QueryGenerator.initQueryWrapper(baseOrderProject, req.getParameterMap());
        Page<BaseOrderProject> page = new Page<BaseOrderProject>(pageNo, pageSize);
        IPage<BaseOrderProject> pageList = baseOrderProjectService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 查询订单核销项目列表
     *
     * @param orderCode
     * @param memberName
     * @param memberPhone
     * @param beginTime
     * @param endTime
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "查询订单列表")
    @ApiOperation(value = "查询订单列表", notes = "查询订单列表")
    @GetMapping(value = "/searchOrderProjectWriteoff")
    public Result<?> searchOrderProjectWriteoff(@RequestParam(name = "orderCode", defaultValue = "") String orderCode,
                                                @RequestParam(name = "memberName", defaultValue = "") String memberName,
                                                @RequestParam(name = "memberPhone", defaultValue = "") String memberPhone,
                                                @RequestParam(name = "beginTime", defaultValue = "") String beginTime,
                                                @RequestParam(name = "endTime", defaultValue = "") String endTime,
                                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                HttpServletRequest req) {
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId + token);
        if (oConvertUtils.isEmpty(storeId)) {
            return Result.error("请先到首页选择店铺");
        }
        if (storeId.contains("过期")) {
            return Result.error("店铺已过期，续费后才可操作");
        }

        Page<BaseOrderProjectWriteoff> page = new Page<BaseOrderProjectWriteoff>(pageNo, pageSize);
        IPage<BaseOrderProjectWriteoff> pageList = baseOrderProjectService.searchOrderProjectWriteoff(page, storeId, orderCode, memberName, memberPhone,
                beginTime, endTime);
        return Result.OK(pageList);
    }

    /**
     * 查询会员可用项目列表
     */
    @RequestMapping(value = "/searchCanUseProjectList", method = RequestMethod.GET)
    public Result<List<BaseOrderProjectMain>> searchCanUseProjectList(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "storeId", required = true) String storeId,
            @RequestParam(name = "memberId", required = true) String memberId) {
        Result<List<BaseOrderProjectMain>> result = new Result<>();
        List<BaseOrderProjectMain> list = baseOrderProjectService.searchCanUseProjectList(pageNo, 10, storeId, memberId);
        result.setSuccess(true);
        result.setResult(list);
        return result;
    }

    /**
     * 查询会员已用完项目列表
     */
    @RequestMapping(value = "/searchHasUsedProjectList", method = RequestMethod.GET)
    public Result<List<BaseOrderProjectMain>> searchHasUsedProjectList(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "storeId", required = true) String storeId,
            @RequestParam(name = "memberId", required = true) String memberId) {
        Result<List<BaseOrderProjectMain>> result = new Result<>();
        List<BaseOrderProjectMain> list = baseOrderProjectService.searchHasUsedProjectList(pageNo, 10, storeId, memberId);
        result.setSuccess(true);
        result.setResult(list);
        return result;
    }


    /**
     * 添加
     *
     * @param baseOrderProject
     * @return
     */
    @AutoLog(value = "订单项目表-添加")
    @ApiOperation(value = "订单项目表-添加", notes = "订单项目表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody BaseOrderProject baseOrderProject) {
        String projectId = baseOrderProject.getProjectId();
        if (projectId != null) {
            ProjectManagement project = projectManagementService.getById(projectId);
            if (project != null) {
                if (baseOrderProject.getIndate() == null) {
                    baseOrderProject.setIndate(project.getIndate());
                    baseOrderProject.setTimingWay(project.getTimingWay());
                    if (baseOrderProject.getValidateTime() == null && "1".equals(project.getTimingWay())) {
                        LocalDate l = LocalDate.now();
                        l=l.plusDays(Integer.parseInt(project.getIndate()));
                        baseOrderProject.setValidateTime(DateUtils.local2util(l));
                    }
                }

            }
        }
        baseOrderProject.setIsValid("1");
        baseOrderProjectService.save(baseOrderProject);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param baseOrderProject
     * @return
     */
    @AutoLog(value = "订单项目表-编辑")
    @ApiOperation(value = "订单项目表-编辑", notes = "订单项目表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody BaseOrderProject baseOrderProject) {
        baseOrderProjectService.updateById(baseOrderProject);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "订单项目表-通过id删除")
    @ApiOperation(value = "订单项目表-通过id删除", notes = "订单项目表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        baseOrderProjectService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "订单项目表-批量删除")
    @ApiOperation(value = "订单项目表-批量删除", notes = "订单项目表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.baseOrderProjectService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "订单项目表-通过id查询")
    @ApiOperation(value = "订单项目表-通过id查询", notes = "订单项目表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        BaseOrderProject baseOrderProject = baseOrderProjectService.getById(id);
        if (baseOrderProject == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(baseOrderProject);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param baseOrderProject
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BaseOrderProject baseOrderProject) {
        return super.exportXls(request, baseOrderProject, BaseOrderProject.class, "订单项目表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BaseOrderProject.class);
    }

}
